package com.arpost.pages;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;

import com.qualcomm.vuforia.samples.VideoPlayback.R;

public class AnimationController {

	private static final String TAG = "ANIMATION_CONTROLLER";

	private Handler mHandler;
	private Runnable mRunnable;

	private Bitmap[] mBitmapBuffer;
	private int mBufferIndex;

	private ImageView mImageView;
	private int[] mResourceIds;
	private int mIndex;
	private int mTotal;
	private int mIntervalSeconds = 40;

	private boolean mFirstRun = true;

	private Context mContext;

	public AnimationController(ImageView imageView, Context context) {
		mImageView = imageView;
		mContext = context;
		mIndex = mTotal = 0;

		mBitmapBuffer = new Bitmap[2];
		setRunnable();
	}

	public void setPNGSequence(int[] resIds) {
		mResourceIds = resIds;
		mIndex = 0;
		mTotal = mResourceIds.length;

		mBufferIndex = 0;
	}

	public void start() {
		mFirstRun = true;
		mHandler.post(mRunnable);
	}

	public void stop() {
		mHandler.removeCallbacks(mRunnable);
	}

	private void setRunnable() {
		mHandler = new Handler();

		mRunnable = new Runnable() {
			@Override
			public void run() {
				if (mBufferIndex == 0 && mBitmapBuffer[1] != null)
					mBitmapBuffer[1].recycle();
				else if (mBitmapBuffer[0] != null)
					mBitmapBuffer[0].recycle();
				if (mFirstRun) { // first time run to prepare the bitmap
					mBitmapBuffer[mBufferIndex] = BitmapFactory.decodeResource(mContext.getResources(), mResourceIds[mIndex]);
					mFirstRun = false;
				}

				// set bitmap
				mImageView.setImageBitmap(mBitmapBuffer[mBufferIndex]);

				// index operations
				if (mBufferIndex == 0)
					mBufferIndex = 1;
				else
					mBufferIndex = 0;
				mIndex += 1;
				if (mIndex == mTotal)
					mIndex = 0;

				mHandler.postDelayed(this, mIntervalSeconds);

				// prepare bitmap for next frame
				mBitmapBuffer[mBufferIndex] = BitmapFactory.decodeResource(mContext.getResources(), mResourceIds[mIndex]);

			}
		};
	}
	
	/*
	public static int [] getRabbitRes(int index) {
		if (index == 0) {
			return RABBIT_RES_1;
		} else if (index == 1) {
			return RABBIT_RES_2;
		} else if (index == 2) {
			return RABBIT_RES_3;
		} else if (index == 3) {
			return RABBIT_RES_4;
		}
		return null;
	}
	
	public static int [] getTurtleRes(int index) {
		if (index == 0) {
			return TURTLE_RES_1;
		} else if (index == 1) {
			return TURTLE_RES_2;
		} else if (index == 2) {
			return TURTLE_RES_3;
		} else if (index == 3) {
			return TURTLE_RES_4;
		}
		return null;
	}

	private static int[] RABBIT_RES_1 = { R.drawable.rabbit_01_00000, R.drawable.rabbit_01_00001, R.drawable.rabbit_01_00002, R.drawable.rabbit_01_00003, R.drawable.rabbit_01_00004,
			R.drawable.rabbit_01_00005, R.drawable.rabbit_01_00006, R.drawable.rabbit_01_00007, R.drawable.rabbit_01_00008, R.drawable.rabbit_01_00009, R.drawable.rabbit_01_00010,
			R.drawable.rabbit_01_00011, };

	private static int[] RABBIT_RES_2 = { R.drawable.rabbit_02_00000, R.drawable.rabbit_02_00002, R.drawable.rabbit_02_00004, R.drawable.rabbit_02_00006, R.drawable.rabbit_02_00008,
			R.drawable.rabbit_02_00010, R.drawable.rabbit_02_00012, R.drawable.rabbit_02_00014, R.drawable.rabbit_02_00016, R.drawable.rabbit_02_00018, R.drawable.rabbit_02_00020,
			R.drawable.rabbit_02_00022, R.drawable.rabbit_02_00024, R.drawable.rabbit_02_00026, R.drawable.rabbit_02_00028, R.drawable.rabbit_02_00030, R.drawable.rabbit_02_00032,
			R.drawable.rabbit_02_00034, R.drawable.rabbit_02_00036, R.drawable.rabbit_02_00038, R.drawable.rabbit_02_00040, R.drawable.rabbit_02_00042, R.drawable.rabbit_02_00044,
			R.drawable.rabbit_02_00046, R.drawable.rabbit_02_00047 };

	private static int[] RABBIT_RES_3 = { R.drawable.rabbit_03_00000, R.drawable.rabbit_03_00002, R.drawable.rabbit_03_00004, R.drawable.rabbit_03_00006, R.drawable.rabbit_03_00008,
			R.drawable.rabbit_03_00010, R.drawable.rabbit_03_00012, R.drawable.rabbit_03_00014, R.drawable.rabbit_03_00016, R.drawable.rabbit_03_00018, R.drawable.rabbit_03_00020,
			R.drawable.rabbit_03_00022, R.drawable.rabbit_03_00024, R.drawable.rabbit_03_00026, };

	private static int[] RABBIT_RES_4 = { R.drawable.rabbit_04_00000, R.drawable.rabbit_04_00002, R.drawable.rabbit_04_00004, R.drawable.rabbit_04_00006, R.drawable.rabbit_04_00008,
			R.drawable.rabbit_04_00010, R.drawable.rabbit_04_00012, R.drawable.rabbit_04_00014, R.drawable.rabbit_04_00016, R.drawable.rabbit_04_00018, R.drawable.rabbit_04_00020,
			R.drawable.rabbit_04_00022, R.drawable.rabbit_04_00024, R.drawable.rabbit_04_00025, };

	private static int[] TURTLE_RES_1 = { R.drawable.turtle_01_00000, R.drawable.turtle_01_00002, R.drawable.turtle_01_00004, R.drawable.turtle_01_00006, R.drawable.turtle_01_00008,
			R.drawable.turtle_01_00010, R.drawable.turtle_01_00012, R.drawable.turtle_01_00014, R.drawable.turtle_01_00016, R.drawable.turtle_01_00018, R.drawable.turtle_01_00020,
			R.drawable.turtle_01_00022, R.drawable.turtle_01_00023, };

	private static int[] TURTLE_RES_2 = { R.drawable.turtle_02_00000, R.drawable.turtle_02_00002, R.drawable.turtle_02_00004, R.drawable.turtle_02_00006, R.drawable.turtle_02_00008,
			R.drawable.turtle_02_00010, R.drawable.turtle_02_00012, R.drawable.turtle_02_00014, R.drawable.turtle_02_00016, R.drawable.turtle_02_00018, R.drawable.turtle_02_00020,
			R.drawable.turtle_02_00022, R.drawable.turtle_02_00024, R.drawable.turtle_02_00026, R.drawable.turtle_02_00028, R.drawable.turtle_02_00030, R.drawable.turtle_02_00032,
			R.drawable.turtle_02_00034, R.drawable.turtle_02_00036, R.drawable.turtle_02_00038, R.drawable.turtle_02_00040, R.drawable.turtle_02_00042, R.drawable.turtle_02_00044,
			R.drawable.turtle_02_00046, };

	private static int[] TURTLE_RES_3 = { R.drawable.turtle_03_00000, R.drawable.turtle_03_00002, R.drawable.turtle_03_00004, R.drawable.turtle_03_00006, R.drawable.turtle_03_00008,
			R.drawable.turtle_03_00010, R.drawable.turtle_03_00012, R.drawable.turtle_03_00014, R.drawable.turtle_03_00016, R.drawable.turtle_03_00018, R.drawable.turtle_03_00020,
			R.drawable.turtle_03_00022, R.drawable.turtle_03_00024, R.drawable.turtle_03_00026, R.drawable.turtle_03_00028, R.drawable.turtle_03_00030, R.drawable.turtle_03_00032,
			R.drawable.turtle_03_00034, R.drawable.turtle_03_00036, R.drawable.turtle_03_00038, R.drawable.turtle_03_00040, R.drawable.turtle_03_00042, R.drawable.turtle_03_00044,
			R.drawable.turtle_03_00046, R.drawable.turtle_03_00048, R.drawable.turtle_03_00050, R.drawable.turtle_03_00052, R.drawable.turtle_03_00054, R.drawable.turtle_03_00056,
			R.drawable.turtle_03_00058, R.drawable.turtle_03_00059, R.drawable.turtle_03_00060, R.drawable.turtle_03_00061, R.drawable.turtle_03_00062, R.drawable.turtle_03_00063,
			R.drawable.turtle_03_00064, R.drawable.turtle_03_00065, R.drawable.turtle_03_00066, R.drawable.turtle_03_00067, R.drawable.turtle_03_00068, R.drawable.turtle_03_00069,
			R.drawable.turtle_03_00070, R.drawable.turtle_03_00071, R.drawable.turtle_03_00072, R.drawable.turtle_03_00073, R.drawable.turtle_03_00074, R.drawable.turtle_03_00075,
			R.drawable.turtle_03_00076, R.drawable.turtle_03_00077, R.drawable.turtle_03_00078, R.drawable.turtle_03_00079, };

	private static int[] TURTLE_RES_4 = { R.drawable.turtle_04_00000, R.drawable.turtle_04_00001, R.drawable.turtle_04_00002, R.drawable.turtle_04_00003, R.drawable.turtle_04_00004,
			R.drawable.turtle_04_00005, R.drawable.turtle_04_00006, R.drawable.turtle_04_00007, R.drawable.turtle_04_00008, R.drawable.turtle_04_00009, R.drawable.turtle_04_00010,
			R.drawable.turtle_04_00011, R.drawable.turtle_04_00012, R.drawable.turtle_04_00013, R.drawable.turtle_04_00014, R.drawable.turtle_04_00015, R.drawable.turtle_04_00016,
			R.drawable.turtle_04_00017, R.drawable.turtle_04_00018, R.drawable.turtle_04_00019, R.drawable.turtle_04_00020, R.drawable.turtle_04_00021, R.drawable.turtle_04_00022,
			R.drawable.turtle_04_00023, R.drawable.turtle_04_00024, R.drawable.turtle_04_00025, R.drawable.turtle_04_00026, R.drawable.turtle_04_00027, R.drawable.turtle_04_00028,
			R.drawable.turtle_04_00029, R.drawable.turtle_04_00030, R.drawable.turtle_04_00031, R.drawable.turtle_04_00032, R.drawable.turtle_04_00033, R.drawable.turtle_04_00034,
			R.drawable.turtle_04_00035, R.drawable.turtle_04_00036, R.drawable.turtle_04_00037, R.drawable.turtle_04_00038, R.drawable.turtle_04_00039, R.drawable.turtle_04_00040,
			R.drawable.turtle_04_00041, R.drawable.turtle_04_00042, R.drawable.turtle_04_00043, R.drawable.turtle_04_00044, R.drawable.turtle_04_00045, R.drawable.turtle_04_00046,
			R.drawable.turtle_04_00047, R.drawable.turtle_04_00048, R.drawable.turtle_04_00049, R.drawable.turtle_04_00050, R.drawable.turtle_04_00051, R.drawable.turtle_04_00052,
			R.drawable.turtle_04_00053, R.drawable.turtle_04_00054, R.drawable.turtle_04_00055, R.drawable.turtle_04_00056, R.drawable.turtle_04_00057, R.drawable.turtle_04_00058,
			R.drawable.turtle_04_00059, R.drawable.turtle_04_00060, R.drawable.turtle_04_00061, R.drawable.turtle_04_00062, R.drawable.turtle_04_00063, R.drawable.turtle_04_00064,
			R.drawable.turtle_04_00065, R.drawable.turtle_04_00066, R.drawable.turtle_04_00067, R.drawable.turtle_04_00068, R.drawable.turtle_04_00069, R.drawable.turtle_04_00070,
			R.drawable.turtle_04_00071, R.drawable.turtle_04_00072, R.drawable.turtle_04_00073, R.drawable.turtle_04_00074, R.drawable.turtle_04_00075, R.drawable.turtle_04_00076,
			R.drawable.turtle_04_00077, R.drawable.turtle_04_00078, R.drawable.turtle_04_00079, R.drawable.turtle_04_00080, R.drawable.turtle_04_00081, R.drawable.turtle_04_00082,
			R.drawable.turtle_04_00083, R.drawable.turtle_04_00084, R.drawable.turtle_04_00085, R.drawable.turtle_04_00086, R.drawable.turtle_04_00087, R.drawable.turtle_04_00088,
			R.drawable.turtle_04_00089, R.drawable.turtle_04_00090, R.drawable.turtle_04_00091, R.drawable.turtle_04_00092, R.drawable.turtle_04_00093, R.drawable.turtle_04_00094, };
			*/
}
